#pragma once
#include "noncopyable.h"
#include <memory>
#include <string>
#include <atomic>
#include "InetAddress.h"
#include "Callbacks.h"
#include "Buffer.h"
#include "Timestamp.h"
class Channel;
class EventLoop;
class Socket;

/**
 * @brief TcpServer => Acceptor => 新用户连接,通过accept拿到通信对端信息和connfd => 
 *         TcpConnection设置回调 => Chanell => Poller检测到事件发生 => Channel事件处理回调操作
 * 
 */


// 用来打包成功连接的服务器和客户端进行通信的链路的
class TcpConnection : private noncopyable, public std::enable_shared_from_this<TcpConnection> 
{
    public:
        TcpConnection( EventLoop *loop,
                        const std::string& name,
                        int sockfd,
                        const InetAddress& localAddr,
                        const InetAddress& peerAddr);
        ~TcpConnection();

        EventLoop *getLoop() const { return loop_; }
        const std::string& name() const { return name_; }
        const InetAddress& localAddress() const { return localAddr_; }
        const InetAddress& peerAddress() const { return peerAddr_; }

        bool connected() const { return state_ == kConnected; }
        bool disconnected() const { return state_ == kDisconnected; }

        // 发送数据
        void send(const std::string& buf);
        // 关闭当前连接
        void shutdown();


        // 设置回调函数
        void setConnectionCallback(const ConnectionCallback& cb) 
        { connectionCallback_ = cb; }
        void setMessageCallback(const MessageCallback& cb) 
        { messageCallback_ = cb; }
        void setWriteCompleteCallback(const WriteCompleteCallback& cb) 
        { writeCompleteCallback_ = cb; }
        void setHighWaterMarkCallback(const HighWaterMarkCallback& cb, size_t highWaterMark) 
        { highWaterMark_ = highWaterMark; highWaterMarkCallback_ = cb; }
        void setCloseCallback(const CloseCallback& cb) 
        { closeCallback_ = cb; }

        void connectEstablished(); //连接建立
        void connectDestroyed(); // 连接销毁

        

    private:

        enum stateE { kDisconnected, kConnecting, kConnected, kDisconnecting }; //连接状态？？
        void setState(stateE state) { state_ = state; }

        void handleRead(Timestamp receiveTime);
        void handleWrite();
        void handleClose();
        void handleError();

       
        void sendInLoop(const void *data, size_t len);

        
        void shutdownInLoop();



        EventLoop* loop_;//tcpconnection是给subloop的，除了设置子线程个数为0
        const std::string name_; //连接名字
        std::atomic_int state_;
        bool reading_;

        std::unique_ptr<Socket> socket_;
        std::unique_ptr<Channel> channel_;

        const InetAddress localAddr_; // 
        const InetAddress peerAddr_;


        //用户给我们的，需要在pendingfunctor处执行，即eventloop.loop中的dopendingfunctor执行
        ConnectionCallback connectionCallback_;
        MessageCallback messageCallback_;
        WriteCompleteCallback writeCompleteCallback_;
        HighWaterMarkCallback highWaterMarkCallback_;
        CloseCallback closeCallback_;
        size_t highWaterMark_;//水位标志  进行流量控制

        Buffer inputBuffer_;
        Buffer outputBuffer_;

};